// import { App } from "./app";
// import {GameStar} from "./GameStar";
// import {BulkLoader} from './utils/BulkLoader';
// import {ImageLoader} from './utils/ImageLoader';
///<reference path='./app.ts'/>
///<reference path='./GameStar.ts'/>
///<reference path='./utils/BulkLoader.ts'/>
///<reference path='./utils/ImageLoader.ts'/>
class Ship extends lg.Container {
    private _fire1: lg.Sprite;
    private _fire2: lg.Sprite;
    private _fire3: lg.Sprite;
    private _ship: lg.Bitmap;
    private _prefixY:number;
    public isBgMove:boolean;
    private light:lg.Sprite;
    constructor() {
        super();
        this.addEventListener("added", this.addedHandler);
    }
    private initView() {
        this.isBgMove=false;
        this._ship = new lg.Bitmap(App.loadManager.getImage("main-body"));
        this.addChild(this._ship);
        var flameSheet: lg.SpriteSheet = new lg.SpriteSheet({ images: ["../res/assets/images/flame.png"], frames: [[0, 0, 48, 59, 0, 24, 0], [48, 0, 48, 59, 0, 24, 0], [0, 59, 48, 58, 0, 24, 0]] });
        this._fire1 = new lg.Sprite(flameSheet);
        this.addChild(this._fire1);
        this._fire1.play();
        this._fire1.framerate = 12;
        this._fire1.x = this._ship.getBounds().width >> 1;
        this._fire1.y = this._ship.getBounds().height;

        this._fire2 = new lg.Sprite(flameSheet);
        this.addChild(this._fire2);
        this._fire2.play();
        this._fire2.framerate = 12;
        this._fire2.x = 12;
        this._fire2.y = this._ship.getBounds().height - 12;
        this._fire2.rotation = 30;

        this._fire3 = new lg.Sprite(flameSheet);
        this.addChild(this._fire3);
        this._fire3.play();
        this._fire3.framerate = 12;
        this._fire3.x = this._ship.getBounds().width - 18;
        this._fire3.y = this._ship.getBounds().height - 10;
        this._fire3.rotation = -30;

        this._prefixY=this.stage.getBounds().height-this.getBounds().height;


        var lightSheet: lg.SpriteSheet = new lg.SpriteSheet({ images: ["../res/assets/images/light.png"], frames: [[0,0,16,16,0,8,8],[16,0,16,16,0,8,8],[0,16,16,16,0,8,8],[16,16,16,16,0,8,8]]});
        this.light = new lg.Sprite(lightSheet);
        this.addChild(this.light);
        this.light.play();
        this.light.x=12+(this._ship.getBounds().width-this.light.getBounds().width>>1);
        this.light.y=-8;
        this.light.scaleY=-1;
        lg.Ticker.addEventListener("tick", () => {

            if (lightSheet.getNumFrames(lightSheet.getAnimations()[0]) == this.light.currentFrame + 1) {

                //播放完时
                //this.light.gotoAndStop(1);
                
            }

        })

        this.setFps(30);
        setInterval(
        ()=>this.loop()
        );
    }
    public blink()
    {
        this.light.play();
    }
    public blinkStop()
    {
        this.light.gotoAndStop(1);
        setTimeout(()=>{ this.light.play();}
        ,2000)
    }
    private loop()
    {
        this._fire1.scaleY=this._fire2.scaleY=this._fire3.scaleY=this._fire1.scaleX=this._fire2.scaleX=this._fire3.scaleX=GameStar.moveY*0.01>1?1:GameStar.moveY*0.01;
        //this.y=GameStar.moveY==0?this._prefixY:this._prefixY-50;
        if(GameStar.moveY!=0)
        {
            if(this.y>this._prefixY-50)
            {
                 this.y--;
            }else
            {
                this.y=this._prefixY-50;
            }
        }else{
            this.y=this._prefixY;
        }
        this.isBgMove=(this.y==this._prefixY-50);
    }
    private addedHandler(e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.initView();

    }

    public setFps(v: number): void {
        this._fire1.framerate = this._fire1.framerate = this._fire1.framerate = v;
    }
}